
/* copyright (c) 1996, 1997, 1998, 1999 William R. Pearson and the
   U. of Virginia */

/* $Name: fasta34t11d4 $ - $Id: manshowbest.c,v 1.16 2001/09/17 21:18:18 wrp Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "defs.h"
#include "structs.h"
#include "param.h"
#ifndef PCOMPLIB
#include "mw.h"
#else
#include "p_mw.h"
#endif

#define MAX_BLINE 200

extern double zs_to_bit(float, int, int);
extern void cal_coord(int n0, int n1, long sq0off, long loffset,
		      struct a_struct *aln);

void showbest (FILE *fp, struct beststr **bptr, int nbest,
	       int qlib, struct mngmsg *m_msg, struct pstruct pst,
	       char *gstring2)
{
  int ntmp = 0;
  int n1tot;
  char bline[MAX_BLINE], fmt[40], pad[MAX_BLINE], rline[40];
  int istart = 0, istop, i = 0, ib;
  int nshow;
  int quiet;
  int r_margin;
  struct beststr *bbp;
  char *bp;

  quiet = 1;


  if (m_msg->aln.llen > MAX_BLINE) m_msg->aln.llen = MAX_BLINE;

  if (pst.zsflag < 0) r_margin = 10;
  else if (pst.zsflag>=0  && m_msg->srelv > 1 ) r_margin = 17;
  else r_margin = 10;

  sprintf(fmt,"%%-%ds (%%4d)",m_msg->aln.llen-r_margin);

  memset(pad,' ',m_msg->aln.llen-(r_margin+6));
  pad[m_msg->aln.llen-(r_margin+12)]='\0';

  if (quiet != -1) {
    nshow = 20;
    if (m_msg->mshow == -1) nshow = nbest;		/* show all */
    /* show specified number */
    else if (m_msg->mshow != 0) {
      nshow = min (m_msg->mshow, nshow);
    }
  }
  else nshow = m_msg->nshow;

  if (quiet==0) istop = 20;
  else istop = nshow;

  if (quiet==0) {
    printf(" How many scores would you like to see? [%d] ",m_msg->nshow);
    fflush(stdout);
    if (fgets(rline,20,stdin)==NULL) exit(0);
    nshow = m_msg->nshow;
    if (rline[0]!='\n' && rline[0]!=0) sscanf(rline,"%d",&nshow);
    if (nshow<=0) nshow = min(20,nbest);
  }

  if ((bp = strchr (m_msg->libstr, '\n')) != NULL) *bp = '\0';
/*   fprintf (fp, "%3d >%s\n", qlib,m_msg->libstr); */

  if (pst.zsflag >= 0)
      if (m_msg->z_bits==1) {/* show bit score */
	fprintf(fp,"The best scores are:%s%s bits E(%ld)",
		pad,m_msg->label,m_msg->db.entries);
      }
      else {/* show z-score */
	fprintf(fp,"The best scores are:%s%s z-sc E(%ld)",
		pad,m_msg->label,m_msg->db.entries);
      }
  else
    fprintf(fp,"The best scores are:%s%s",pad,m_msg->label);

  if (m_msg->markx==9)
    fprintf(fp,"\t%%_id  %%_gid %4s  alen  an0  ax0  pn0  px0  an1  ax1  pn1  px1 gapq gapl  fs ",m_msg->f_id1);
  fprintf(fp,"\n");
  
  istart = 0;
l1:
  istop = min(nbest,nshow);
  for (ib=istart; ib<istop; ib++) {
  bbp = bptr[ib];
  if (quiet==1 && pst.zsflag>=0  && bbp->escore > m_msg->e_cut) {
    nshow = ib;
    goto done;
  }

  strncpy(bline,bbp->desptr->bline,m_msg->aln.llen-r_margin);

  bline[m_msg->aln.llen-r_margin-1]='\0';

  if (m_msg->nframe < 0) 
    bline[m_msg->aln.llen-r_margin]='\0';
  else
    bline[m_msg->aln.llen-(r_margin+4)]='\0';

  n1tot =  (bbp->desptr->n1tot_p) ? *bbp->desptr->n1tot_p : bbp->n1;
  fprintf (fp, fmt,bp=bline,n1tot);
  if (m_msg->nframe > 1) fprintf (fp, " [%d]", bbp->frame+1);
  else if (m_msg->nframe >= 0) fprintf(fp," [%c]",(bbp->frame?'r':'f'));
  if (m_msg->srelv == 1) 
    fprintf (fp, " %4d", bbp->score[pst.score_ix]);
  else if (m_msg->srelv > 0) fprintf (fp, " %4d", bbp->score[0]);
  if (m_msg->srelv > 1) fprintf (fp, " %4d", bbp->score[1]);
  if (m_msg->srelv > 2) fprintf (fp, " %4d", bbp->score[2]);
  if (pst.zsflag>=0) {
    if (m_msg->z_bits==1) 
      fprintf (fp, " %4.0f %7.2lg",zs_to_bit(bbp->zscore,m_msg->n0,bbp->n1),bbp->escore);
    else
      fprintf (fp, " %4.1f %7.2lg",bbp->zscore,bbp->escore);
  }

  if (m_msg->markx == 9) {
    cal_coord(m_msg->n0,bbp->n1,m_msg->sq0off,bbp->desptr->loffset-1,bbp->aln_d);

    fprintf(fp,"\t%5.3f %5.3f %4d %4d %4ld %4ld %4ld %4ld %4ld %4ld %4ld %4ld %3d %3d %3d",
	    bbp->percent/100.0,bbp->gpercent/100.0, bbp->swscore,
	    bbp->aln_d->a_len,
	    bbp->aln_d->d_start0,bbp->aln_d->d_stop0,
	    m_msg->sq0off,m_msg->sq0off+m_msg->n0-1,
	    bbp->aln_d->d_start1,bbp->aln_d->d_stop1,
	    bbp->desptr->loffset, bbp->desptr->loffset+bbp->n1-1,
	    bbp->aln_d->ngap_q,bbp->aln_d->ngap_l,bbp->aln_d->nfs);
  }
  fprintf (fp, "\n");
  fflush(fp);
  }

  if (quiet==0) {
    printf(" More scores? [0] ");
    fflush(stdout);
    if (fgets(rline,20,stdin)==NULL) exit(0);
    ntmp = 0;
    if (rline[0]!='\n' && rline[0]!=0) sscanf(rline,"%d",&ntmp);
    if (ntmp<=0) ntmp = 0;
    if (ntmp>0) {
      istart = istop;
      nshow += ntmp;
      goto l1;
    }
  }
  else if (quiet == 1)
    if (ib < nbest && 
	((pst.zsflag>=0 && bbp->escore < m_msg->e_cut) /* || pst.zsflag <=0 */)) {
      if (m_msg->mshow_flg && istop >= m_msg->mshow) goto done;
      istart=istop;
      nshow += 10;
      goto l1;
    }

 done:
  m_msg->nshow = nshow;
  if (fp!=stdout) fprintf(fp,"\n");
}
